/*
 * LoginForm.java
 *
 * Created on 24 listopad 2008, 16:27
 */
package wat.clientapp.main;

import java.security.InvalidKeyException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import wat.ziszcommon.clientgui.MyJPanel;
import org.jdesktop.application.SingleFrameApplication;
import wat.ziszcommon.invoker.MethodException;
import wat.clientapp.connection.SecureConnection;
import wat.ziszcommon.invoker.UnauthorizedAccesException;
import wat.ziszcommon.remote.RemoteModuleFactory;
import wat.ziszcommon.transport.Module;

/**
 *
 * @author  kubaolszewski
 */
public class LoginForm extends javax.swing.JFrame {

    private SingleFrameApplication app;

    /** Creates new form LoginForm */
    public LoginForm(SingleFrameApplication app) {
        this.setTitle("Projekt ZISZ");
        initComponents();
        this.app = app;
        try {
            RemoteModuleFactory.newInstance(SecureConnection.getInstance());
        } catch (InvalidKeyException ex) {
            Logger.getLogger(LoginForm.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
   // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
   private void initComponents() {

      jButton1 = new javax.swing.JButton();
      userLogin = new javax.swing.JTextField();
      userPassword = new javax.swing.JPasswordField();
      jPanel1 = new javax.swing.JPanel();
      jLabel1 = new javax.swing.JLabel();
      jLabel2 = new javax.swing.JLabel();
      jLabel3 = new javax.swing.JLabel();
      statusLabel = new javax.swing.JLabel();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
      setName("Form"); // NOI18N

      org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(LoginForm.class);
      jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
      jButton1.setName("jButton1"); // NOI18N
      jButton1.addActionListener(new java.awt.event.ActionListener() {
         public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton1ActionPerformed(evt);
         }
      });

      userLogin.setText(resourceMap.getString("userLogin.text")); // NOI18N
      userLogin.setName("userLogin"); // NOI18N

      userPassword.setText(resourceMap.getString("userPassword.text")); // NOI18N
      userPassword.setName("userPassword"); // NOI18N

      jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
      jPanel1.setName("jPanel1"); // NOI18N

      jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
      jLabel1.setName("jLabel1"); // NOI18N

      javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
      jPanel1.setLayout(jPanel1Layout);
      jPanel1Layout.setHorizontalGroup(
         jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(176, 176, 176)
            .addComponent(jLabel1)
            .addContainerGap(149, Short.MAX_VALUE))
      );
      jPanel1Layout.setVerticalGroup(
         jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(jPanel1Layout.createSequentialGroup()
            .addGap(59, 59, 59)
            .addComponent(jLabel1)
            .addContainerGap(70, Short.MAX_VALUE))
      );

      jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
      jLabel2.setName("jLabel2"); // NOI18N

      jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
      jLabel3.setName("jLabel3"); // NOI18N

      statusLabel.setText(resourceMap.getString("statusLabel.text")); // NOI18N
      statusLabel.setAutoscrolls(true);
      statusLabel.setName("statusLabel"); // NOI18N

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addGap(143, 143, 143)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
               .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
               .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGap(18, 18, 18)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
               .addComponent(userPassword)
               .addComponent(userLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 146, Short.MAX_VALUE))
            .addContainerGap())
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
         .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(229, Short.MAX_VALUE)
            .addComponent(jButton1)
            .addGap(169, 169, 169))
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(statusLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
            .addContainerGap())
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGroup(layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel2)
               .addComponent(userLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
               .addComponent(jLabel3)
               .addComponent(userPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addComponent(jButton1)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
            .addComponent(statusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
      );

      pack();
   }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
        try {

            SecureConnection sc = SecureConnection.getInstance();
            char[] c = userPassword.getPassword();
            Map<String, Module> clients = sc.login(userLogin.getText(), userPassword.getText());

            Set<String> clientNames = clients.keySet();

            System.out.println("Liczba dostepnych modulow: " + clients.size());

            for (String clientName : clientNames) {
                System.out.println("Zaladowanao klienta: " + clientName);
            }

            app.show(new ZISZView(app, clients));
            this.setVisible(false);

        } catch (InvalidKeyException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Błąd uzgadniania PKI", JOptionPane.ERROR_MESSAGE);
            Logger.getLogger(LoginForm.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MethodException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Błąd", JOptionPane.ERROR_MESSAGE);
            this.userLogin.setText("");
            this.userPassword.setText("");
            this.statusLabel.setText("Błąd połączenia z serwerem");
        } catch (UnauthorizedAccesException ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage(), "Błąd autoryzacji", JOptionPane.ERROR_MESSAGE);
            this.statusLabel.setText("Błąd logowania");
            this.userPassword.setText("");
        }
    }
    /**
     * @param args the command line arguments
     */
   // Variables declaration - do not modify//GEN-BEGIN:variables
   private javax.swing.JButton jButton1;
   private javax.swing.JLabel jLabel1;
   private javax.swing.JLabel jLabel2;
   private javax.swing.JLabel jLabel3;
   private javax.swing.JPanel jPanel1;
   private javax.swing.JLabel statusLabel;
   private javax.swing.JTextField userLogin;
   private javax.swing.JPasswordField userPassword;
   // End of variables declaration//GEN-END:variables
}
